import { schema } from '@kbn/config-schema';
import { IRouter, IKibanaResponse, ResponseError } from '../../../../src/core/server';
import { API_PREFIX } from '../../common';
import { DefaultBackend } from '../adaptors/default_backend';

let BACKEND = new DefaultBackend();

export function TestDataRouter(router: IRouter){
  router.post(
    {
      path: `${API_PREFIX}/testData`,
      validate: {
        body: schema.object({
          name: schema.string(),
        }),
      },
    },
    async (context, request, response): Promise<IKibanaResponse<any | ResponseError>> => {
      let addResponse = {};
      try {
        addResponse = await BACKEND.createTestData(context, {name : "testData"});
        return response.ok({
          body: addResponse,
        });
      } catch (error) {
        return response.custom({
          statusCode: error.statusCode || 500,
          body: error.message,
        });
      }
    }
  );

    router.get(
      {
        path: `${API_PREFIX}/testData/{testDataId}`,
        validate: {
          params: schema.object({
            testDataId: schema.string(),
          }),
        },
      },//
      async (context, request, response): Promise<IKibanaResponse<any | ResponseError>> => {
        try {
          const testDataInfo = await BACKEND.getTestData(context, request.params.testDataId);
          return response.ok({
            body: testDataInfo,
          });
        } catch (error) {
          return response.custom({
            statusCode: error.statusCode || 500,
            body: error.message,
          });
        }
      }
    );


    router.get(
      {
        path: '/api/plugin_test/example',
        validate: false,
      },
      async (context, request, response) => {
        return response.ok({
          body: {
            time: new Date().toISOString(),
          },
        });
      }
    );
}


